package com.youlu.campus.admin.task;

import com.youlu.campus.service.platform.PlatformUserComplaintConsultRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class PlatformUserComplaintTask {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private PlatformUserComplaintConsultRecordService recordService;

    /**
     * 2分钟执行一次
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void updateStatus() {
        Date date = new Date();
        Boolean flag = redisTemplate.opsForValue().setIfAbsent("platform.user.complaint:status", "1", 10L, TimeUnit.MINUTES);
        log.info("开始执行:>>> 更新平台投诉状态锁状态:{}", flag);
        if (Objects.nonNull(flag) && Boolean.FALSE.equals(flag)) {
            return;
        }
        recordService.updateReadMsgStatus(date, "visitor");
        Boolean dLock = redisTemplate.delete("platform.user.complaint:status");
        log.info("结束执行:>>> 更新平台投诉状态锁状态释放锁状态:{},耗时:{} 毫秒", dLock, (new Date().getTime() - date.getTime()) / 1000);
    }
}
